package com.smartens.boit.adapter;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.smartens.boit.R;
import com.smartens.boit.bean.FaultBean;
import com.smartens.boit.bean.ListBean;

import java.util.List;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.InnerHolder> {
    private final List<ListBean> mData;
    private OnItemClickListener mOnItemClickListener;

    public ListAdapter(List<ListBean> data) {
        this.mData = data;
    }

    //    创建条目的View
    @NonNull
    @Override
    public ListAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        穿进去的View就是条目界面
        View view = View.inflate(parent.getContext(), R.layout.list_view,null);
        return new InnerHolder(view);
    }

    //     设置数据
    @Override
    public void onBindViewHolder(@NonNull ListAdapter.InnerHolder holder, int position) {
        holder.setData(mData.get(position), position);

    }

    // 返回条目个数
    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
//        设置监听
        this.mOnItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        TextView mName;
        TextView mNumber;
        TextView mTime;
        private int mPosition;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
//            找到条目控件
            mName = itemView.findViewById(R.id.list_name);
            mNumber = itemView.findViewById(R.id.list_number);
            mTime = itemView.findViewById(R.id.list_time);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(mPosition);
                    }
                }
            });
        }

        public void setData(ListBean listBean, int position) {
            this.mPosition = position;
            mName.setText(listBean.getDeviceName());
            mNumber.setText(listBean.getDeviceNum());
            String[] strs = listBean.getFaultTime().split(" ");
            mTime.setText(strs[0]);
        }
    }


}
